<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		
		<link rel="stylesheet" type="text/css" href="../../style.css" />
		
		<script type="text/javascript" src="../../navigation_menu.js"></script>
		<script type="text/javascript" src="../../validation.js"></script>
		
		<script type="text/javascript">
		    var djConfig = {
				isDebug:true,
				parseOnLoad:true,
				usePlainJson:true
		    };
		</script>
		 
		<script type="text/javascript" src="../../dojo/dojo/dojo.js"></script>
		<script type="text/javascript" src="../../dojo/dijit/dijit.js"></script>

		<script type="text/javascript">
    		dojo.require("dijit.form.Button");
    		dojo.require("dojo.rpc.JsonService");    	
    	</script>
		
		<script language="JavaScript" type="text/javascript">
			//获得服务
			var orderPlanImportService = new dojo.rpc.JsonService("OrderPlanImportService?smd");

			//页面加载时的一些操作
			dojo.addOnLoad(load);
			function load() {
				menu_main('../../nav.xml', 'navigation_menu');
			}
			//验证日期的合法性
			function validate_date(id) {
				if (isDate(document.getElementById(id).value) == false) {
					document.getElementById(id + "_msg").innerHTML = "请输入正确的日期";
					return false;
				} else {
					document.getElementById(id + "_msg").innerHTML = "";
					return true;
				}
			}
			//验证表单合法性
			function validate_order_plan() {
				var res = true;
				var date1, date2;

				//检查是否有数据输入
				if (checkEmpty(document.getElementById("id").value) == true) {
					document.getElementById("id_msg").innerHTML = "此项必填";
					res = false;
				}
				if (checkEmpty(document.getElementById("scd").value) == true) {
					document.getElementById("scd_msg").innerHTML = "此项必填";
					res = false;
				}
				//验证日期合法性
				if (validate_date("start") == false) {
					res = false;
				}
				if (validate_date("end") == false) {
					res = false;
				}
				if (validate_date("cancel") == false) {
					res = false;
				}
				//比较日期先后关系是否符合要求
				date1 = document.getElementById("start").value;
				date2 = document.getElementById("end").value;
				if (compareDate(date1, date2) == 1) {
					document.getElementById("start_msg").innerHTML = "订舱开始时间应在订舱截止时间前！";
					document.getElementById("end_msg").innerHTML = "订舱截止时间应在订舱开始时间后！";
					res = false;
				} else if (compareDate(date1, date2) != 2) {
					document.getElementById("start_msg").innerHTML = "";
					document.getElementById("end_msg").innerHTML = "";
				}
				date1 = document.getElementById("cancel").value;
				if (compareDate(date2, date1) == 1) {
					document.getElementById("end_msg").innerHTML = "订舱截止时间应在退舱截止时间前！";
					document.getElementById("cancel_msg").innerHTML = "退舱截止时间应在订舱截止时间后！";
					res = false;
				} else if (compareDate(date2, date1) != 2) {
					document.getElementById("end_msg").innerHTML = "";
					document.getElementById("cancel_msg").innerHTML = "";
				}
				//检验箱数是否，正整数，总箱数大于等于大箱数
				var total = document.getElementById("total").value;
				var big = document.getElementById("big").value;
				if (isInteger(total) == false) {
					document.getElementById("total_msg").innerHTML = "请输入正整数";
					res = false;
				} else if (checkEmpty(document.getElementById("total").value) == true) {
					document.getElementById("total_msg").innerHTML = "此项必填";
					res = false;
				} else {
					document.getElementById("total_msg").innerHTML = "";
				}
				if (isInteger(big) == false) {
					document.getElementById("big_msg").innerHTML = "请输入正整数";
					res = false;
				} else if (checkEmpty(document.getElementById("big").value) == true) {
					document.getElementById("big_msg").innerHTML = "此项必填";
					res = false;
				} else {
					document.getElementById("big_msg").innerHTML = "";
				}
				if (isInteger(total) == true && isInteger(big) == true) {
					var totalInt = parseInt(total, 10);
					var bigInt = parseInt(big, 10);
					if (totalInt < bigInt) {
						document.getElementById("total_msg").innerHTML = "总箱数应大于等于大箱数";
						document.getElementById("big_msg").innerHTML = "大箱数应小于等于总箱数";
						res = false;
					} else {
						document.getElementById("total_msg").innerHTML = "";
						document.getElementById("big_msg").innerHTML = "";
					}
				}
				return res;
			}
			//导入函数
			function import_order_plan() {
				if(validate_order_plan() == true) {
					orderPlanImportService.post(document.getElementById("id").value,
							document.getElementById("scd").value,
							document.getElementById("start").value,
							document.getElementById("end").value,
							document.getElementById("cancel").value,
							myParseInt(document.getElementById("total").value),
							myParseInt(document.getElementById("big").value),
							myParseInt(document.getElementById("gp20").value),
							myParseInt(document.getElementById("gp40").value),
							myParseInt(document.getElementById("hq40").value)
							).addCallback(import_order_plan_response);
				} else {
					alert("数据有误，请重新输入！");
				}
			}
			//导入操作的响应函数
			function import_order_plan_response(answer) {
				alert(answer);
			}
			//我的parseInt函数
			function myParseInt(value) {
				if (checkEmpty(value)) {
					return -1;
				} else if (!isInteger(value)) {
					return -1;
				} else {
					return parseInt(value);
				}
			}
			//查询函数
			function query_order_plan() {
				orderPlanImportService.query(document.getElementById("id").value,
						document.getElementById("scd").value,
						document.getElementById("start").value,
						document.getElementById("end").value,
						document.getElementById("cancel").value,
						myParseInt(document.getElementById("total").value),
						myParseInt(document.getElementById("big").value),
						myParseInt(document.getElementById("gp20").value),
						myParseInt(document.getElementById("gp40").value),
						myParseInt(document.getElementById("hq40").value)).addCallback(query_order_plan_response);
			}
			//查询响应函数，显示查询结果
			function query_order_plan_response(answer) {
				console.dir(answer);
				//为表格:查询结果，添加一行数据
				function add_query_answer(id, scd, start, end, cancel,
						total, big, small) {
					//tbl：表格，tr：行，td：数据，anchor：链接
					var tbl, tr, td, anchor;

					//得到想要添加行的表格
					tbl = document.getElementById("query_answer");
					//创建一行
					tr = document.createElement("tr");

					td = document.createElement("td");
					anchor = document.createElement("a");
					//设置属性
					anchor.href = "#";//"make_box_alloc_form.html?id=" + id;
					//直接赋值anchor.onclick = 'alert("bla...ba...")';不好使
					anchor.setAttribute("onclick", 'show_order_plan_details(' + id + ')');
					//将数据加入anchor中
					anchor.appendChild(document.createTextNode("详情"));
					//讲anchor加入td中
					td.appendChild(anchor);
					tr.appendChild(td);
					
					//创建一个数据单元
					td = document.createElement("td");
					//将数据加入到数据单元中
					td.appendChild(document.createTextNode(id));
					//将数据单元加入到行中
					tr.appendChild(td);

					td = document.createElement("td");
					anchor = document.createElement("a");
					//设置属性
					anchor.href = "#";
					//直接赋值anchor.onclick = 'alert("bla...ba...")';不好使
					anchor.setAttribute("onclick", 'alert("anchor的onclick属性好使！")');
					//将数据加入anchor中
					anchor.appendChild(document.createTextNode(scd));
					//讲anchor加入td中
					td.appendChild(anchor);
					tr.appendChild(td);

					td = document.createElement("td");
					td.appendChild(document.createTextNode(start));
					tr.appendChild(td);

					td = document.createElement("td");
					td.appendChild(document.createTextNode(end));
					tr.appendChild(td);

					td = document.createElement("td");
					td.appendChild(document.createTextNode(cancel));
					tr.appendChild(td);

					td = document.createElement("td");
					td.appendChild(document.createTextNode(total));
					tr.appendChild(td);

					td = document.createElement("td");
					td.appendChild(document.createTextNode(big));
					tr.appendChild(td);

					td = document.createElement("td");
					td.appendChild(document.createTextNode(small));
					tr.appendChild(td);

					//将行加入表格中
					tbl.tBodies[0].appendChild(tr);
				}
				//清空表格函数
				function clear_query_answer() {
					var rows = document.getElementById("query_answer").tBodies[0].rows;
					for (var i = rows.length - 1; i > 0; i--) {
						var rIndex = rows[i].sectionRowIndex;
						rows[i].parentNode.deleteRow(rIndex);
					}
				}

				//在显示查询结果前，先清空之前的查询结果
				clear_query_answer();
				//以参数形式传过来的二维数组可以正常使用
				for (var i in answer.list) {
					add_query_answer(answer.list[i].ID, answer.list[i].shipSchedule.id, answer.list[i].startTimeString,
							answer.list[i].endTimeString, answer.list[i].cancelTimeString, answer.list[i].totalBox, answer.list[i].bigBox,
							parseInt(answer.list[i].totalBox) - parseInt(answer.list[i].bigBox));
				}
			}
			//显示OrderPlan详情
			function show_order_plan_details(id) {
				//跳转到缮制舱位分配清单页面
				//此处应对id进行转换以使参数形式更广泛				
				window.open("make_box_alloc_form.html?id=" + id/*, "_self"*/);
			}
		</script>
		
		<title>导入舱位预分配计划</title>
	</head>

	<body>
		<!-- 导航菜单 -->
		<div id="navigation_menu" class="nav"></div>
		<div class="content">
			<h1>导入舱位预分配计划</h1>
				
			<div id="order_plan_import">
				<!-- 订舱计划表单 -->
				<fieldset>
				<legend>订舱计划</legend>
				<form id="order_plan">
<table>
<tr>
					<td><label for="id">计划编号：</label></td>
					<td><input type="text" name="id" id="id"><a id="id_msg"></a></td>
</tr>
<tr>
					<td><label for="scd">航次：</label></td>
					<td><input type="text" name="scd" id="scd"><a id="scd_msg"></a></td>
</tr>
<tr>
					<td><label for="start">订舱开始时间(MM/DD/YYYY)：</label></td>
					<td><input type="text" name="start" id="start" maxlength="10"><a id="start_msg"></a></td>
</tr>				
<tr>	
					<td><label for="end">订舱截止时间(MM/DD/YYYY)：</label></td>
					<td><input type="text" name="end" id="end" maxlength="10"><a id="end_msg"></a></td>
</tr>
<tr>
					<td><label for="cancel">退舱截止时间(MM/DD/YYYY)：</label></td>
					<td><input type="text" name="cancel" id="cancel" maxlength="10"><a id="cancel_msg"></a></td>
</tr>
<tr>
					<td><label for="total">总箱数：</label></td>
					<td><input type="text" name="total" id="total"><a id="total_msg"></a></td>
</tr>	
<tr>
					<td><label for="big">大箱数：</label></td>
					<td><input type="text" name="big" id="big"><a id="big_msg"></a></td>
</tr>	
<tr>
					<td><label for="gp20">箱型GP20：</label></td>
					<td><input type="text" name="gp20" id="gp20"><a id="gp20_msg"></a></td>
</tr>	
<tr>
					<td><label for="gp40">箱型GP40：</label></td>
					<td><input type="text" name="gp40" id="gp40"><a id="gp40_msg"></a></td>
</tr>	
<tr>
					<td><label for="hq40">箱型HQ40：</label></td>
					<td><input type="text" name="hq40" id="hq40"><a id="hq40_msg"></a></td>
</tr>	
</table>
					<input type="button" name="import" value="导入" onclick="import_order_plan();">	
					<input type="button" name="query" value="查询" onclick="query_order_plan();">
					<input type="reset" name="reset" value="重置">
				</form>
				</fieldset>
			</div>
				
			<div id="order_plan_query">
				<!-- 查询结果 -->
			<fieldset>
				<legend>查询结果</legend>
				<table id="query_answer" border="1">
					<tr>
						<th>操作</th>
						<th>计划编号</th>
						<th>航次</th>
						<th>订舱开始时间</th>
						<th>订舱截止时间</th>
						<th>退舱截止时间</th>
						<th>总箱数</th>
						<th>大箱数</th>
						<th>小箱数</th>
					</tr>
				</table>
			</fieldset>
			</div>
		</div>
	</body>
</html>
